package homework.bh1506;

import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

/*【编号：1506】在某次考试中，学生的成绩信息如下： 姓名 年龄 成绩 Tom 20 90 Jerry 22 95 John 20 100 Lily 22 100
        Lucy 22 90 Kevin 22 90 请分别用Comparable和Comparator两个接口对以上同学的成绩做降序排序，如果成绩一样，
        那在成绩排序的基础上按照年龄由小到大排序，成绩和年龄都一样，则按照姓名的字典顺序排序。*/
public class Test3 {
    public static void main(String[] args) {
        TreeSet<Student> sts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
               int result = o1.getAge() - o2.getAge();
                result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
                return result;
            }
        });
        Student s1 = new Student("Tom", 20, 90);
        Student s2 = new Student("Jerry", 22, 95);
        Student s3 = new Student("John", 20, 100);
        Student s4 = new Student("Lily", 22, 100);
        Student s5 = new Student("Lucy", 22, 90);
        Student s6 = new Student("Kevin", 22, 90);
        Collections.addAll(sts, s1, s2, s3, s4, s5, s6);
        for (Student st : sts) {
            System.out.println(st);
        }
    }
}

class Student implements Comparable<Student> {
    private String name;
    private int age;
    private int cji;
    public Student() {
    }
    public Student(String name, int age, int cji) {
        this.name = name;
        this.age = age;
        this.cji = cji;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getCji() {
        return cji;
    }
    public void setCji(int cji) {
        this.cji = cji;
    }
    @Override
    public int compareTo(Student o) {
        //首先根据成绩进行从大到小做降序排序
        int result = o.cji - this.cji;
        result = result == 0 ? this.name.compareTo(o.getName()) : result;
        return result;
    }
    @Override
    public String toString() {
        return  "姓名：" + name +"    年龄：" + age + "    成绩：" + cji;
    }
}